/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.core.fadingmessage.calibration.wizard;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.mopore.jat.core.fadingmessage.calibration.CalibrationInfo;
import org.mopore.jat.gui.SingleRoundCornerRectangleFactory.CornerType;
import org.mopore.jat.gui.wizard.AbstractWizardPage;
import org.mopore.jat.gui.wizard.ButtonPanel;

import static org.mopore.jat.core.fadingmessage.calibration.wizard.CalibrationIllustration.createIllustrationImage;

public class ChooseCornerPage extends AbstractWizardPage<CalibrationInfo> {
	
	private CornerType selectedCorner;
	
	private final static Map<CornerType, ImageIcon> iconMap =
		new HashMap<CornerType, ImageIcon>(){
		{
			for (CornerType cornerType : CornerType.values() ) {
				fillIconToMap( cornerType );
			}
		}

		private void fillIconToMap( CornerType cornerType ) {
			BufferedImage image = createIllustrationImage( cornerType );
			put( cornerType, new ImageIcon( image ) );
		}
	};
	private JLabel imageLabel;
	
	public ChooseCornerPage() {
		
		ButtonPanel buttonPanel = getButtonPanel();
		buttonPanel.addActionToBackButton(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				changePage( IntroductionPage.class );
			}
		});
		buttonPanel.enableBackButton();
		buttonPanel.addActionToNextButton(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				changePage( PerformCalibtrationPage.class );
			}
		});
		buttonPanel.enableNextButton();
	}


	@Override
	public JPanel getContentPanel(){
		
		CalibrationInfo progress = context().get();
		selectedCorner = progress.getCornerType();
		
		JPanel panel = new JPanel();
		
		panel.setLayout( null );
		
		JLabel chooseLabel = 
			new JLabel( "Step 1: Choose a screen corner for the message window." );
		chooseLabel.setBounds(7, 10, 320, 15 );
		panel.add( chooseLabel );
		
		JButton upperLeftButton = new JButton( "upper left" );
		upperLeftButton.setBounds( 5, 30, 100, 40 );
		upperLeftButton.setHorizontalAlignment( SwingConstants.LEFT );
		upperLeftButton.setVerticalAlignment( SwingConstants.TOP );
		upperLeftButton.addActionListener( 
				new SelectCornerActionListner( CornerType.UPPER_LEFT )
		);
		panel.add( upperLeftButton );

		JButton upperRightButton = new JButton( "upper right" );
		upperRightButton.setBounds( 225, 30, 100, 40 );
		upperRightButton.setHorizontalAlignment( SwingConstants.RIGHT );
		upperRightButton.setVerticalAlignment( SwingConstants.TOP );
		upperRightButton.addActionListener( 
				new SelectCornerActionListner( CornerType.UPPER_RIGHT )
		);
		panel.add( upperRightButton );
		
		JButton lowerLeftButton = new JButton( "lower left" );
		lowerLeftButton.setBounds( 5, 180, 100, 40 );
		lowerLeftButton.setHorizontalAlignment( SwingConstants.LEFT );
		lowerLeftButton.setVerticalAlignment( SwingConstants.BOTTOM );
		lowerLeftButton.addActionListener( 
				new SelectCornerActionListner( CornerType.LOWER_LEFT )
		);
		panel.add( lowerLeftButton );

		JButton lowerRightButton = new JButton( "lower right (recommended)" );
		lowerRightButton.setBounds( 145, 180, 180, 40 );
		lowerRightButton.setHorizontalAlignment( SwingConstants.RIGHT );
		lowerRightButton.setVerticalAlignment( SwingConstants.BOTTOM );
		lowerRightButton.addActionListener( 
				new SelectCornerActionListner( CornerType.LOWER_RIGHT )
		);
		panel.add( lowerRightButton );
		
		BufferedImage image = CalibrationIllustration.createIllustrationImage( 
				selectedCorner );
		Icon icon = new ImageIcon( image );
		imageLabel = new JLabel( icon );
		imageLabel.setBounds( 60, 85, 200, 80 );
		panel.add( imageLabel );
		
		panel.setBorder( BorderFactory.createEmptyBorder( 0, 10, 0, 10) );
		
		return panel;
	}


	@Override
	public void storeToContext() {
		CalibrationInfo progress = context().get();
		progress.setCornerType( selectedCorner );
	}
	

	private class SelectCornerActionListner implements ActionListener{

		private final CornerType cornerType;

		public SelectCornerActionListner(CornerType cornerType) {
			this.cornerType = cornerType;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			ImageIcon icon = iconMap.get( cornerType );
			ChooseCornerPage.this.imageLabel.setIcon( icon );
			ChooseCornerPage.this.selectedCorner = cornerType;
		}
		
	}

}
